#!/bin/bash
#
# http://www.oracle.com/technology/pub/articles/smiley_10gdb_install.html
# Redhat/Fedora install
#
#
#
# Base functions
#
# function to print da shit (pds, get it?!)
pds() {
	echo $1
}
# Base variables
# You must change something, or little less here. Maybe nothing!
CLEAR_CMD=/usr/bin/clear
LINE_DELIMITATOR="+------------------------------------------------------------+"
LINE_DELIMITATOR_BLANK="|	                                                     |"
# Clear things out
$CLEAR_CMD

# Starts output
echo $LINE_DELIMITATOR
echo -e "| \033[1mRedhat/Fedora pre-install script for \E[31m\033[1mORACLE RDMBS / AS\033[0m \033[0m    |"
echo "|	                                                     |"
echo -e "| \E[47;34m\033[1m[oracle-prep-env]\033[0m                                          |"
echo "| Autor: Rosivaldo Azevedo (rosivaldo[at]gmail.com)          |"
echo "|      : Jefferson Aranha (jefferson.aranha[at]gmail.com)    |"
echo $LINE_DELIMITATOR

ORACLE_USER=oracle
ORACLE_DBA_GROUP=dba
ORACLE_INSTALL_GROUP=oinstall
ORACLE_BASE="/u01/app/oracle"

# Read oracle software owner
read -p "Owner ["$ORACLE_USER"]: " ANSWER
if [ ! -z $ANSWER ]; then
	ORACLE_USER=$ANSWER
fi
# Read oracle software owner group
read -p "Owner group ["$ORACLE_INSTALL_GROUP"]: " ANSWER
if [ ! -z $ANSWER ]; then
	ORACLE_INSTALL_GROUP=$ANSWER
fi
# Read oracle DBA group
read -p "DBA group ["$ORACLE_DBA_GROUP"]: " ANSWER
if [ ! -z $ANSWER ]; then
	ORACLE_DBA_GROUP=$ANSWER
fi

echo $ORACLE_USER"-"$ORACLE_INSTALL_GROUP"-"$ORACLE_DBA_GROUP

# Read oracle software path
read -p "Oracle base ["$ORACLE_BASE"]: " ANSWER
if [ ! -z $ANSWER ]; then
	ORACLE_BASE=$ANSWER
fi

# Get the first directory
ORACLE_BASE_MOUNT_POINT=${ORACLE_BASE%/*/*}
# Haven't found another way, if there is more then three sub-directories
ORACLE_BASE_MOUNT_POINT=${ORACLE_BASE_MOUNT_POINT%/*/*}

echo $ORACLE_BASE"-"$ORACLE_BASE_MOUNT_POINT

# Check processor arch
ARCH=$(uname -i)
if [ $ARCH == "x86_64" ]; then
	rpm -q --qf '%{NAME}-%{VERSION}-%{RELEASE}  (%{ARCH})\n' \
	binutils compat-db compat-libstdc++-33 compat-libstdc++-33 \
	compat-libstdc++-296 control-center gcc gcc-c++ glibc glibc \
	glibc-common glibc-devel glibc-devel glibc-headers gnome-libs \
	libgcc libaio libgcc libstdc++ libstdc++-devel libgomp make \
	pdksh sysstat
else
	rpm -q binutils compat-db control-center gcc gcc-c++ glibc glibc-common \
	gnome-libs libstdc++ libstdc++-devel make pdksh sysstat xscreensaver libaio openmotif21
fi


# for x86_64

MEMORY=$(grep MemTotal /proc/meminfo)
MEMORY=$(echo $MEMORY | cut -d ' ' -f 2)
MEMORY=$(echo "$MEMORY/1024/1024" | bc)
echo "memoria: "$MEMORY"GB"

SWAP=$(grep SwapTotal /proc/meminfo)
SWAP=$(echo $SWAP | cut -d ' ' -f 2)
SWAP=$(echo "$SWAP/1024/1024" | bc)
echo "swap: "$SWAP"GB"

exit
/usr/sbin/groupadd $ORACLE_INSTALL_GROUP
/usr/sbin/groupadd $ORACLE_DBA_GROUP
/usr/sbin/useradd -m -g $ORACLE_INSTALL_GROUP -G $ORACLE_DBA_GROUP $ORACLE_USER
id $ORACLE_USER
passwd $ORACLE_USER

# Change base ownership and permissions
mkdir -p $ORACLE_BASE
chown -R $ORACLE_USER:$ORACLE_INSTALL_GROUP $ORACLE_BASE_MOUNT_POINT
chmod -R 775 $ORACLE_BASE_MOUNT_POINT

cp /etc/sysctl.conf /etc/sysctl.conf.old
cp /etc/security/limits.conf /etc/security/limits.conf.old
cp /etc/pam.d/login /etc/pam.d/login.old
cp /etc/profile /etc/profile.old
cp /etc/csh.login /etc/csh.login.old

cat >> /etc/sysctl.conf <<EOF
kernel.shmall = 2097152
kernel.shmmax = 4294967295
kernel.shmmni = 4096
kernel.sem = 256 32000 100 142
fs.file-max = 206173
net.ipv4.ip_local_port_range = 1024 65000
net.core.rmem_default=262144
net.core.wmem_default=262144
net.core.rmem_max=262144
net.core.wmem_max=262144
kernel.msgmni = 2878 
kernel.msgmax = 8192 
kernel.msgmnb = 65535
EOF
/sbin/sysctl -p

cat >> /etc/security/limits.conf <<EOF
$ORACLE_USER soft nproc 2047
$ORACLE_USER hard nproc 16384
$ORACLE_USER soft nofile 1024
$ORACLE_USER hard nofile 65536
EOF

cat >> /etc/pam.d/login <<EOF
session required /lib/security/pam_limits.so
EOF

cat >> /etc/profile <<EOF
if [ \$USER = "$ORACLE_USER" ]; then 
 if [ \$SHELL = "/bin/ksh" ]; then
 ulimit -p 16384
 ulimit -n 65536
 else
 ulimit -u 16384 -n 65536
 fi
 umask 022
fi
EOF

cat >> /etc/csh.login <<EOF
if ( \$USER == "$ORACLE_USER" ) then
 limit maxproc 16384
 limit descriptors 65536
 umask 022
endif
EOF
